package com.sskj.gcc.bean;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

public class UiUtil2 {
    private String TAG = "UiUtil";

    private int pxToDp(int px, Context c) {
        DisplayMetrics displayMetrics = c.getResources().getDisplayMetrics();
        return Math.round(px * (displayMetrics.ydpi / DisplayMetrics.DENSITY_DEFAULT));
    }

    public float dpToPx(int dp, Context context) {
        return dp * context.getResources().getDisplayMetrics().density;
    }

    private int getStatusBarHeight(Resources r) {
        int resourceId = r.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return r.getDimensionPixelSize(resourceId);
        } else {
            return 0;
        }

    }

    private int getNavBarHeight(Context ct) {
        return getNavigationBarSize(ct).y;
    }

    private Point getNavigationBarSize(Context context) {
        Point appUsableSize = getAppUsableScreenSize(context);
        Point realScreenSize = getScreenSize(context);

        // navigation bar on the right
        if (appUsableSize.x < realScreenSize.x) {
            return new Point(realScreenSize.x - appUsableSize.x, appUsableSize.y);
        }

        // navigation bar at the bottom
        if (appUsableSize.y < realScreenSize.y) {
            return new Point(appUsableSize.x, realScreenSize.y - appUsableSize.y);
        } else {
            return new Point();
        }

        // navigation bar is not present
    }

    private Point getAppUsableScreenSize(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        return size;
    }

    private Point getScreenSize(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        Point size = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            display.getRealSize(size);
        }
        return size;
    }

    private int rotateBy(int current, int degrees) {
        // TODO: 21/08/16 a better way should exist
        /*int rotation = current + degrees;
    if (rotation > 359) rotation -=360;
    if (rotation < 0) rotation +=360;*/
        return (current + degrees) % 360;
    }
}
